<?php


namespace App\process;


use Helper\Config;
use Helper\Mysql;
use Helper\Redis1;
use Helper\Reg;
use Swoole\Timer;

class ChatClear
{

    static public function start($server){

        Timer::tick(1000 ,function (){

            //每日24点开始清理
            if(date('His') == "000000"){
                //删除聊天信息
                Mysql::getInstance()
                    ->where("to_days(FROM_UNIXTIME(create_at,'%Y-%m-%d'))<=to_days(NOW())-7")
                    ->delete('user_chat_record');

                //删除资源
                $date = date('Y-m-d',strtotime("-7 day"));
                $chat_img_dir = Config::getInstance()->get('filesystem')['img_path'].$date;
                $chat_video_dir = Config::getInstance()->get('videosystem')['video_path'].$date;
                if(is_dir($chat_img_dir)){
                    exec("rm -rf $chat_img_dir");
                }
                if(is_dir($chat_video_dir)){
                    exec("rm -rf $chat_video_dir");
                }
            }


        });

        //定期清理日志文件
        Timer::tick(1000 ,function (){

            //每日24点开始备份
            if(date('His') == "000000"){

                $date = date('Y-m-d');
                $logFile  = Config::getInstance()->get('server')['setting']['log_file'];
                $document_root  = Config::getInstance()->get('server')['setting']['document_root'];
                if(is_file($logFile)){
                    //先备份
                    exec("cp -r $logFile  {$document_root}Log/{$date}.log");
                    //再清空
                    exec("echo '' > $logFile");

                }
            }


        });


    }

}